package com.chb.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chb.system.mapper.NoticeMapper;
import com.chb.system.service.NoticeService;
import com.chb.system.service.UserService;
import com.chb.system.vo.NoticeVo;
import com.hos.entity.sys.Notice;
import com.hos.entity.sys.Role;
import com.hos.entity.sys.User;
import com.hos.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ：张家琪
 * @date ：Created in 2023/12/16 10:01
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class NoticeServiceImpl implements NoticeService {
    @Resource
    private NoticeMapper noticeMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private UserService userService;

    /**
     * 查询所有可用公告通知
     *
     * @return
     */
    @Override
    public List<Notice> selecAlltNotice() {
        // String s = redisTemplate.opsForValue().get("Notice:main");
        // if (StringUtils.hasText(s)) {
        //     return JSON.parseArray(s, Notice.class);
        // }
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Notice::getStatus, 0);
        wrapper.orderByDesc(Notice::getCreateTime);
        // redisTemplate.opsForValue().set("Notice:main", JSON.toJSONString(wrapper));
        return noticeMapper.selectList(wrapper);
    }

    /**
     * 条件查询通知公告
     *
     * @param noticeVo 通知公告VO类
     * @return
     */
    @Override
    public Page<Notice> findNoticeByCond(NoticeVo noticeVo) {
        Page<Notice> page = new Page<>(noticeVo.getPageNum(), noticeVo.getPageSize());
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(noticeVo.getNoticeTitle())) {
            wrapper.like(Notice::getNoticeTitle, noticeVo.getNoticeTitle());
        }

        if (StringUtils.hasText(noticeVo.getCreateBy())) {
            wrapper.like(Notice::getCreateBy, noticeVo.getCreateBy());
        }
        if (StringUtils.hasText(noticeVo.getNoticeType())) {
            wrapper.like(Notice::getNoticeType, noticeVo.getNoticeType());
        }
        if (StringUtils.hasText(noticeVo.getStatus())) {
            wrapper.eq(Notice::getStatus, noticeVo.getStatus());
        }
        wrapper.orderByDesc(Notice::getCreateTime);
        return noticeMapper.selectPage(page, wrapper);
    }

    @Override
    public Integer addNotice(Notice notice) {
        // 创建时间
        notice.setCreateTime(new Date());
        User user = getUserByToken();
        notice.setCreateBy(user.getUserName());
        return noticeMapper.insert(notice);
    }

    @Override
    public Integer deleteNoticeByIds(List<Long> ids) {
        return noticeMapper.deleteBatchIds(ids);
    }

    @Override
    public Notice getNoticeById(Long noticeId) {
        return noticeMapper.selectById(noticeId);
    }

    @Override
    public Integer updateNotice(Notice notice) {
        return noticeMapper.updateById(notice);
    }

    /**
     * @return com.hos.entity.sys.User
     * @create by: 陈海彬
     * @description: 获取请求用户的信息
     * @create time: 2023/12/12 10:39
     */
    private User getUserByToken() {
        String token = WebUtil.getToken();
        String phone = redisTemplate.opsForValue().get("user:" + token);
        return userService.findUserByPhone(phone);
    }
}